﻿using Cheems.HMI.Models;
using Cheems.HMI.ViewModels.Interface;
using Cheems.Interface.Configuration;
using Prism.Ioc;
using Prism.Mvvm;
using Prism.Regions;
using System.Windows;
using System.Windows.Media;

namespace Cheems.HMI.ViewModels
{
    public class AppVM : BindableBase, IAppVM
    {
        internal IRegionManager _regionManager;
        internal IContainerProvider _containerProvider;

        public AppVM(IRegionManager regionManager, IContainerProvider containerProvider)
        {
            this._regionManager = regionManager;
            this._containerProvider = containerProvider;

            _containerProvider.Resolve<IConfig<HMIConfig>>().OnConfigChanged += AppVM_OnConfigChanged;

            LoadHMIConfig(); // 初始化加载配置
            WindowBackColor.Freeze();
        }

        /// <summary>
        /// 配置文件发生改变
        /// </summary>
        private void AppVM_OnConfigChanged(HMIConfig obj)
        {
            LoadHMIConfig();
        }

        /// <summary>
        /// 加载HMI配置
        /// </summary>
        private void LoadHMIConfig()
        {
            // 加载并更新配置
            Title = _containerProvider.Resolve<IConfig<HMIConfig>>().GetConfig().AppName;
            WindowBackColor = CreateFrozenBrush(_containerProvider.Resolve<IConfig<HMIConfig>>().GetConfig().WindowBackColor);
            AppBackColor = CreateFrozenBrush(_containerProvider.Resolve<IConfig<HMIConfig>>().GetConfig().AppBackColor);
            WindowForeground = CreateFrozenBrush(_containerProvider.Resolve<IConfig<HMIConfig>>().GetConfig().WindowForeground);
            WindowTitleForeground = CreateFrozenBrush(_containerProvider. Resolve<IConfig<HMIConfig>>().GetConfig().WindowTitleForeground);
            WindowCornerRadius = _containerProvider.Resolve<IConfig<HMIConfig>>().GetConfig().WindowCornerRadius;
            WindowFontSize = _containerProvider.Resolve<IConfig<HMIConfig>>().GetConfig().WindowFontSize;
            MainWindowSize = _containerProvider.Resolve<IConfig<HMIConfig>>().GetConfig().MainWindowSize;
            ChildWindowSize = _containerProvider.Resolve<IConfig<HMIConfig>>().GetConfig().ChildWindowSize;
            InputCardWindowSize = _containerProvider.Resolve<IConfig<HMIConfig>>().GetConfig().InputCardWindowSize;

        }


        private string _title;
        /// <summary>
        /// 标题
        /// </summary>
        public string Title
        {
            get { return _title; }
            set { SetProperty(ref _title, value); }
        }

        private Brush _windowBackColor;
        /// <summary>
        /// 窗体颜色
        /// </summary>
        public Brush WindowBackColor
        {
            get { return _windowBackColor; }
            set { SetProperty(ref _windowBackColor, value); }
                
        }

        private Brush _appBackColor;
        /// <summary>
        /// 软件主体背景色
        /// </summary>
        public Brush AppBackColor
        {
            get { return _appBackColor; }
            set { SetProperty(ref _appBackColor, value); }
        }

        private Brush _windowForeground;
        /// <summary>
        /// 软件字体颜色
        /// </summary>
        public Brush WindowForeground
        {
            get { return _windowForeground; }
            set { SetProperty(ref _windowForeground, value); }
        }

        private Brush _windowTitleForeground;
        /// <summary>
        /// 软件窗体标题字体颜色
        /// </summary>
        public Brush WindowTitleForeground
        {
            get { return _windowTitleForeground; }
            set { SetProperty(ref _windowTitleForeground, value); }
        }

        private CornerRadius _windowCornerRadius;
        /// <summary>
        /// 窗体圆角
        /// </summary>
        public CornerRadius WindowCornerRadius
        {
            get { return _windowCornerRadius; }
            set { SetProperty(ref _windowCornerRadius, value); }
        }
        private double _windowFontSize;
        /// <summary>
        /// 窗体字体大小
        /// </summary>
        public double WindowFontSize
        {
            get { return _windowFontSize; }
            set { SetProperty(ref _windowFontSize, value); }
        }
        private WindowSize _mainWindowSize;
        /// <summary>
        /// 主窗体大小
        /// </summary>
        public WindowSize MainWindowSize
        {
            get { return _mainWindowSize; }
            set { SetProperty(ref _mainWindowSize, value); }
        }
        private WindowSize _childWindowSize;
        /// <summary>
        /// 子窗体大小
        /// </summary>
        public WindowSize ChildWindowSize
        {   
            get { return _childWindowSize; }
            set { SetProperty(ref _childWindowSize, value); }
        }
        private WindowSize _inputCardWindowSize;
        /// <summary>
        /// 输入框大小
        /// </summary>
        public WindowSize InputCardWindowSize
        {
            get { return _inputCardWindowSize; }
            set { SetProperty(ref _inputCardWindowSize, value); }
        }

        private SolidColorBrush CreateFrozenBrush(Color color)
        {
            var brush = new SolidColorBrush(color);
            brush.Freeze();
            return brush;
        }
    }
}
